<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %>
<%@ page import="java.util.*" %>
<%@ page import="inet.db.video.*" %>
<%@ page import="java.math.*" %>
<jsp:useBean id="videoDAO" class="inet.db.video.VideoDAO" scope="session"/>
<jsp:useBean id="videoCatDAO" class="inet.db.video.VideoCategoryDAO" scope="session"/>
<link href="include/style.css" rel="stylesheet" type="text/css"/>
 <%
   request.setCharacterEncoding("utf-8");	 
   String sCatId   = request.getParameter("catId");
   String sCatName=request.getParameter("catName");
   String sPage  = request.getParameter("page");
   String sRows  = request.getParameter("rows");
   if (sPage == null || "".equals(sPage.trim())) {
       sPage = "1"; //first page
   }
   if (sRows == null || "".equals(sRows.trim())) {
       sRows = "10";
   }
   BigDecimal catId =null; 
   if (sCatId != null) {
       catId = new BigDecimal(sCatId); 
   }
   int totalRows  = videoDAO.getCount(catId,true);							  
   int currPage = Integer.parseInt(sPage);
   int rowsPerPage = Integer.parseInt(sRows);
   int numOfPages = totalRows / rowsPerPage;
   if (totalRows % rowsPerPage > 0) numOfPages++;
   if(sCatId==null && sCatName==null){
   		Collection cCats = videoCatDAO.findAllRoots(1);
      	if (cCats != null && cCats.size() > 0) {
          VideoCategory cat = null;
          int i = 0;
          for (Iterator it = cCats.iterator(); it.hasNext(); i++) {
              cat = (VideoCategory) it.next();
              if (catId == null) catId = cat.getId();
              if (catId.equals(cat.getId())) sCatName = cat.getName();
              }
          }    
   }
%>  
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
			 <tr>
	  		 	<td colspan="8" height="20" bgcolor="#F0F0F0" align="left" class="bluedark-text1">
	  		 	<%=sCatName.toUpperCase() %>
	  		 	</td>
	  		 </tr>	
	  		 <tr>
				<td height="5" colspan="8"></td>
			 </tr>
			 <tr>	
		
							
                            
                            <tr>
                            <td>
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
									  <tr>
										<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
											  <tr>
												<td width="11"><img src="images/corner-ketban1.gif"/></td>
												<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
													  <tr>
														<td width="474"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
															  <tr>
																<td bgcolor="eaeaea" height="5" width="297"></td>
															  </tr>
															  <tr>
																<td height="5"></td>
															  </tr>
															</table></td>
													  </tr>
													</table>
												</td>
												<td width="11"><img src="images/corner-ketban2.gif"/></td>
											  </tr>
											</table>
										</td>
									  </tr>
									  <tr>
										<td bgcolor="eaeaea"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
											  <tr>
												<td width="5">&nbsp;</td>
												
												<td bgcolor="#FFFFFF" height="280" valign="top">
                                                
                                                <table>
                                              
                                                	<%
														Collection cVideos=videoDAO.findByPage(catId, currPage, rowsPerPage,true);
															 if(cVideos!=null && cVideos.size()>0){
															 Iterator it=cVideos.iterator();
															 Video video=null; 
															 while(it.hasNext()){
																video=(Video)it.next();	  
															
													 %>
                                                	<tr>
								<td width="10">&nbsp;</td>
								<td width="110"><table width="100%"  border="0" cellspacing="1" cellpadding="0" bgcolor="eaeaea">
													<tr>
														<td height="110"><img src="<%=request.getContextPath() %>/videoImage?id=<%=video.getId() %>" border="0" height="112" width="112"></td>
													</tr>
												</table>
								</td>
								<td width="10">&nbsp;</td>
								<td valign="top" width="269"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
												  <tr>
													<td align="left" class="black-text2" height="25"><b>Tên Clip :</b> <span class="titlenews-link"><%=(video.getName()!=null)? video.getName():"" %></span></td>
												  </tr>
												  <tr>
													<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
														  <tr>
															<td width="50%" align="left" class="black-text2" height="22"><b>Dung lượng: </b> <%=video.getSize()%>kb </td>
															<td width="10%" align="center"><%=(video.getDuration()!=null)? "<img src=\"images/BallClockAmber.png\">":""%></td>
															<td align="left" class="black-text2"><%=(video.getDuration()!=null)? video.getDuration():"" %></td>
														  </tr>
														</table>
														</td>
												  </tr>
												  <tr>
													<td align="left" class="black-text2" height="22"><b>Lượt tải: </b> <%=video.getDownload()%></td>
												  </tr>
												  <tr>
													<td align="left" class="black-text2" height="22"><b>Mã số: </b> 9<%=video.getId() %> </td>
												  </tr>
												</table>
												</td>
								<%if(it.hasNext()){
									video=(Video)it.next();	  
								 %>			
											<td width="110"><table width="100%"  border="0" cellspacing="1" cellpadding="0" bgcolor="eaeaea">
																<tr>
																	<td height="110"><img src="<%=request.getContextPath() %>/videoImage?id=<%=video.getId() %>" border="0" height="112" width="112"></td>
																</tr>
															</table>
											</td>
											<td width="10">&nbsp;</td>
											<td valign="top" width="269"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
												  <tr>
													<td align="left" class="black-text2" height="25"><b>Tên Clip :</b> <span class="titlenews-link"><%=(video.getName()!=null)? video.getName():""%></span></td>
												  </tr>
												  <tr>
													<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
														  <tr>
															<td width="50%" align="left" class="black-text2" height="22"><b>Dung lượng: </b> <%=video.getSize() %>kb </td>
															<td width="10%" align="center"><%=(video.getDuration()!=null)? "<img src=\"images/BallClockAmber.png\">":""%></td>
															<td align="left" class="black-text2"><%=(video.getDuration()!=null)? video.getDuration():"" %></td>
														  </tr>
														</table>
														</td>
												  </tr>
												  <tr>
													<td align="left" class="black-text2" height="22"><b>Lượt tải: </b> <%=video.getDownload() %></td>
												  </tr>
												  <tr>
													<td align="left" class="black-text2" height="22"><b>Mã số: </b> 9<%=video.getId() %> </td>
												  </tr>
												</table>
												</td>
								<%} else { %>
									<td width="10">&nbsp;</td>
									<td width="110">
									</td>
									<td width="10">&nbsp;</td>
									<td valign="top" width="269">
									</td>
								<%}out.println("<td width=\"10\">&nbsp;</td></tr><tr><td height=\"10\" colspan=\"8\"></td></tr>"); %>  				
								<%
								    }
  								  }   	 
								%>		
								
								  <%if(cVideos!=null && cVideos.size()>0){ %>	  
                             	<tr>
										<%
											int startNo=0;
					 						int endNo=0;	
					 					  int PagesOfSegment=5;
										if(numOfPages>1){
											//Print Previous Page if required
											out.println("<td width='350' align='right' class='bluedark-text1' colspan='23'>Trang");
											int currSegment=currPage/PagesOfSegment;
					 						if(currPage%PagesOfSegment>0)currSegment+=1;
					 						startNo=currSegment*PagesOfSegment-PagesOfSegment+1;
					 						if(currSegment*PagesOfSegment>numOfPages)endNo=numOfPages;
					 						else endNo=currSegment*PagesOfSegment;
											if(currSegment  >1){
												out.print( "<a href='?page=1&catId="+catId+"&catName="+sCatName+"' class='bluedark-text'> Đầu </a>");
												out.print( "<a href='?page=" + (currSegment-1)*PagesOfSegment+"&catId="+catId+"&catName="+sCatName+"' class='bluedark-text'> &lt;&lt;</a>");
											}
											//Print direct access page numbers.
											for(int No=startNo;No<=endNo;No++){
												if (No == currPage){
													out.print("<font color='red'><b>"+No+"</b></font>  ");
												} 
												else{
													out.print( "<a href='?page=" + No +"&catId="+sCatId+"&catName="+sCatName+"' class='bluedark-text'>"+No+"</a>  ");
												}
											}
											//Print Next Page if required		
											if(numOfPages != endNo)
														out.print( " ...<a href='?page="+numOfPages+"&catId="+sCatId+"&catName="+sCatName+"' class='bluedark-text'> "+numOfPages+" </a>");
																			
											if (numOfPages>PagesOfSegment*currSegment){ 
												out.print("<a href='?page="+((currSegment*PagesOfSegment) + 1)+"&catId="+sCatId+"&catName="+sCatName+"' class='bluedark-text'>&gt;&gt;</a>");
												out.print( "<a href='?page="+numOfPages+"&catId="+sCatId+"&catName="+sCatName+"' class='bluedark-text'> Cuối </a>");
													}										
													out.print("</td>");										
												}
												%>
							
										  </tr>
									<%} %> 	 	
								 
                                                </table>
                                                
												</td>
												
												<td width="5">&nbsp;</td>
											
											</table>
										</td>
									  </tr>
									  <tr>
										<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
											  <tr>
												<td width="11"><img src="images/corner-ketban4.gif"/></td>
												<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
													  <tr>
														<td width="474"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
															  <tr>
																<td height="5"></td>
															  </tr>
															  <tr>
																<td bgcolor="eaeaea" height="5" width="297"></td>
															  </tr>
															</table></td>
													  </tr>
													</table>
												</td>
												<td width="11"><img src="images/corner-ketban3.gif"/></td>
											  </tr>
											</table>
										</td>
									  </tr>
									</table>
                            </td>
                            </tr>  

							</table>
